<?php
namespace app\index\controller;
 
 //导入Controller
 use think\Controller;
 //导入数据库
 use think\Db;
 use think\Session;
class Goodsinfo extends Allow
{
	//商品详情
    public function getindex()
    { //创建请求对象
       $request=request();
       //获取商品id
       $id=$request->param('id');
      if(isset($id)){
         //获取评价信息
       $eva=Db::query("SELECT me.tel,ev.time,ev.content FROM `evaluate` ev,`member` me,`pro_goods` pro where ev.uid=me.id and ev.goods_id=pro.id and ev.status=0 and ev.goods_id=$id");
       $count=count($eva);
       for($i=0;$i<$count;$i++){
         $str=substr($eva[$i]['tel'],3,4);
         $eva[$i]['tel']=str_replace($str,"****",$eva[$i]['tel']);
         $eva[$i]['time']=date("Y年m月d日 H:i",$eva[$i]['time']);
       }
      //品牌遍历
      $brand=Db::table("brand")->select();
      
       //获取商品所有数据
       $goodsinfo=Db::table("pro_goods")->where("id",$id)->find();
       // var_dump($goodsinfo);exit;
       //加载商品详情模版
       $coupon=Db::table("coupon")->select();
       return $this->fetch("Goods/goodsinfo",['goodsinfo'=>$goodsinfo,'brand'=>$brand,'eva'=>$eva,'count'=>$count,'coupon'=>$coupon]);
     }else{
        $this->error("未选择商品","/home/index");
     }
  
	 }

    public function getjian(){
      //创建请求对象
      $request=request();
      //商品id
      $id=$request->param('id');
      //页面数量
      $a=$request->param('a');
      //   //获取商品所有数据
      // $goodsinfo=Db::table("pro_goods")->where("id","1")->find();
      // //商品库存
      //  $b=$goodsinfo['num'];
       //判断页面数量是否小于1如果小于让其等于1
       if($a<1){
          $a=1;
       }else{
          $a-=1;
       }

       echo $a;


  }

  public function getjia(){
      //创建请求对象
      $request=request();
      //商品id
      $id=$request->param('id');
      //页面数量
      $a=$request->param('a');
      //获取商品所有数据
      $goodsinfo=Db::table("pro_goods")->where("id",$id)->find();
      //商品库存
      $b=$goodsinfo['num'];
      //判断是否大于库存
       if($a>=$b){
          $a=$b;
       }else{
          $a+=1;
       }

      echo $a;

  }


  //修改优惠卷状态
  public function getcoupon(){
      $request=request();
      $id=$request->param('id');
      $statu['statu']=$request->param('statu');
      if(Db::table('coupon')->where('id',"{$id}")->update($statu)){
        Session::set('coupon',"{$id}");
        // $a=Session::get('coupon');
        echo 1;
      }
  }



}
